package fpt.edu.vn.action;

import java.util.Iterator;
import java.util.List;
import java.util.Map;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

import fpt.edu.vn.dao.TopicsDao;
import fpt.edu.vn.util.sendEmail;

/* 
 * DetailAction.java 
 *  
 * 0.1
 * 
 * 10/15/2012 
 *  
 * Copyright notice 
 *  
 * Modification Logs: 
 *   DATE        AUTHOR      DESCRIPTION 
 *  -------------------------------------------------------- 
 *  15-Oct-2012  ThuongND    Create new 
 */
public class HideTopicAction extends ActionSupport {

	private static final long serialVersionUID = 1L;
	private int topicId;
	private List<Object> checkOwnerTopic;
	private List<Object> checkAdmin;
	private List<Object> topics;
	private int isOwner;
	private int isAdmin;
	private boolean ishide = false;
	private sendEmail send = new sendEmail();

	public boolean isIshide() {
		return ishide;
	}

	public void setIshide(boolean ishide) {
		this.ishide = ishide;
	}

	// For SessionAware
	Map<String, Object> session;

	private TopicsDao dao = new TopicsDao();

	public String execute() {
		String email = "";
		session = ActionContext.getContext().getSession();
		if (session.containsKey("emailss")) {
			email = (String) session.get("emailss");
			// get email, userid to compare with user login
			checkOwnerTopic = dao.checkOwnerTopic(email, topicId);
			checkAdmin = dao.checkAdmin(email);
			if (checkAdmin.size() < 1) {
				setIsAdmin(0);
			} else {
				setIsAdmin(1);
			}
			if (checkOwnerTopic.size() < 1) {
				setIsOwner(0);
			} else {
				setIsOwner(1);
			}

		}
		this.topics = dao.getEmail(topicId);
		if (isAdmin == 1 || isOwner == 1) {
			if (dao.hideTopic(topicId)) {
				ishide = true;
			}
		}
		if (isAdmin == 1 && isOwner == 0) {
			for (Iterator iterator1 = topics.iterator(); iterator1.hasNext();) {
				Object[] employee = (Object[]) iterator1.next();

				send.send_Email(employee[1].toString(), "Bài đăng "
						+ employee[0].toString()
						+ " Của bạn đã bị ẩn bởi admin !",
						"Bài đăng của bạn đã bị ẩn bởi admin !");
			}
		}
		if ((isOwner == 1 && isAdmin == 1)||(isOwner == 1 && isAdmin == 0)) {
			for (Iterator iterator1 = topics.iterator(); iterator1.hasNext();) {
				Object[] employee = (Object[]) iterator1.next();

				send.send_Email(employee[1].toString(), "Bài đăng "
						+ employee[0].toString()
						+ " Của bạn đã được ẩn theo yêu cầu !",
						"Bài đăng bạn đã được ẩn theo yêu cầu !");
			}
		}

		if (ishide) {
			return SUCCESS;
		} else {
			return ERROR;
		}
	}

	public int getTopicId() {
		return topicId;
	}

	public void setTopicId(int topicId) {
		this.topicId = topicId;
	}

	public int getIsOwner() {
		return isOwner;
	}

	public void setIsOwner(int isOwner) {
		this.isOwner = isOwner;
	}

	public int getIsAdmin() {
		return isAdmin;
	}

	public void setIsAdmin(int isAdmin) {
		this.isAdmin = isAdmin;
	}

	public List<Object> getTopics() {
		return topics;
	}

	public void setTopics(List<Object> topics) {
		this.topics = topics;
	}

}
